﻿namespace TestTool.Tests.Common.CommonUtils
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Xml;
    using TestTool.Proxies.Onvif;

    public static class Extensions
    {
        public static Service FindService(this IEnumerable<Service> services, string ns)
        {
            Func<Service, bool> func = null;
            if (services == null)
            {
                return null;
            }
            if (func == null)
            {
                func = s => s.Namespace == ns;
            }
            return Enumerable.FirstOrDefault<Service>(Enumerable.ThenByDescending<Service, int>(Enumerable.OrderByDescending<Service, int>(services, delegate (Service s) {
                if (s.Version != null)
                {
                    return s.Version.Major;
                }
                return 0;
            }), delegate (Service s) {
                if (s.Version != null)
                {
                    return s.Version.Minor;
                }
                return 0;
            }), func);
        }

        public static string GetNamespacePrefix(this XmlElement element, string ns)
        {
            return element.GetNamespacePrefix(ns, string.Empty);
        }

        public static string GetNamespacePrefix(this XmlElement element, string ns, string defaultPrefix)
        {
            string prefixOfNamespace = element.GetPrefixOfNamespace(ns);
            if (string.IsNullOrEmpty(prefixOfNamespace))
            {
                if (!string.IsNullOrEmpty(defaultPrefix) && !element.HasAttribute(string.Format("xmlns:{0}", prefixOfNamespace)))
                {
                    System.Xml.XmlAttribute attribute = element.OwnerDocument.CreateAttribute(string.Format("xmlns:{0}", defaultPrefix));
                    attribute.Value = ns;
                    element.Attributes.Append(attribute);
                    return defaultPrefix;
                }
                int num = 1;
                while (true)
                {
                    prefixOfNamespace = string.Format("tns{0}", num);
                    if (!element.HasAttribute(string.Format("xmlns:{0}", prefixOfNamespace)))
                    {
                        break;
                    }
                    num++;
                }
                System.Xml.XmlAttribute node = element.OwnerDocument.CreateAttribute(string.Format("xmlns:{0}", prefixOfNamespace));
                node.Value = ns;
                element.Attributes.Append(node);
            }
            return prefixOfNamespace;
        }

        public static bool RepresentsTopic(this XmlElement element)
        {
            if (element.HasAttribute("topic", "http://docs.oasis-open.org/wsn/t-1"))
            {
                System.Xml.XmlAttribute attribute = element.Attributes["topic", "http://docs.oasis-open.org/wsn/t-1"];
                if (string.Compare(attribute.Value, "true", StringComparison.CurrentCultureIgnoreCase) == 0)
                {
                    return true;
                }
            }
            return false;
        }
    }
}

